<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import 'echarts/theme/shine.js'
import resize from './mixins/resize'
import { apiCustomerStatPie } from '@/api/stat/customer'

export default {
  name: 'LevelChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    apiCustomerStatPie({}).then(res => {
      this.parseData(res.data)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    // 构建统计数据
    parseData(data) {
      const option = {
        title: {
          text: '客户等级统计',
          subtext: '统计不同等级客户的数量',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '用户等级',
            type: 'pie',
            radius: '50%',
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }

      this.initChart(option)
    },

    initChart(option) {
      this.chart = echarts.init(document.getElementById(this.id), 'shine')
      this.chart.setOption(option)
    }
  }
}
</script>
